home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / 7edit / source / svaerecording.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  30.0 KB  |  1,214 lines

  1. /*
  2.     File:        SVAERecording.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Original version by Jon Lansdell and Nigel Humphreys.
  7.                 3.1 updates by Greg Sutton.
  8.  
  9.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                 You may incorporate this Apple sample source code into your program(s) without
  12.                 restriction. This Apple sample source code has been provided "AS IS" and the
  13.                 responsibility for its operation is yours. You are not permitted to redistribute
  14.                 this Apple sample source code as "Apple sample source code" after having made
  15.                 changes. If you're going to re-distribute the source, we require that you make
  16.                 it clear in the source that the code was descended from Apple sample source
  17.                 code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                 7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24.  
  25. #include "SVAERecording.h"
  26.  
  27. #include "SVEditAEUtils.h"
  28. #include "SVEditUtils.h"
  29. #include "SVAEWindowUtils.h"
  30. #include "SVAETextUtils.h"
  31. #include "SVAppleEvents.h"
  32.  
  33. #include <AEPackObject.h>
  34. #include <TextUtils.h>
  35.  
  36.  
  37.  
  38. static short   gBigBrother;
  39. static char    *gTypingBuffer;
  40. static short   gCharsInBuffer;
  41. static AEDesc  gTypingTargetObject;
  42.  
  43. OSErr    InstallRecordingHandlers(void)
  44. {
  45.     OSErr    err;
  46.     
  47.     gBigBrother = 0;
  48.     gCharsInBuffer = 0;
  49.     gTypingBuffer = (char *)NewPtr(32000);
  50.     gTypingTargetObject.dataHandle = 0;
  51.     
  52.     err = AEInstallEventHandler( kCoreEventClass, kAENotifyStartRecording, NewAEEventHandlerProc(HandleStartRecording), noRefCon, false);
  53.     err = AEInstallEventHandler( kCoreEventClass, kAENotifyStopRecording, NewAEEventHandlerProc(HandleStopRecording), noRefCon, false);
  54.  
  55.     return(err);
  56. }
  57.  
  58. pascal OSErr HandleStartRecording(const AppleEvent *theAppleEvent,
  59.                                                           AppleEvent *reply,
  60.                                                           long       handlerRefCon)        
  61. {
  62. #pragma unused (reply,handlerRefCon)
  63.  
  64.     OSErr myErr;
  65.  
  66.     gBigBrother++;
  67.  
  68.     myErr = GotRequiredParams(theAppleEvent);
  69.  
  70.     return(myErr);
  71.     
  72. } // HandleStartRecording
  73.  
  74. pascal OSErr HandleStopRecording(const AppleEvent *theAppleEvent,
  75.                                                          AppleEvent *reply,
  76.                                                              long handlerRefCon)        
  77. {
  78. #pragma unused (theAppleEvent,reply,handlerRefCon)
  79.  
  80.     gBigBrother--;
  81.     return(noErr);
  82. } // HandleStopRecording
  83.  
  84.  
  85. // Make an object specifier for a window given the WindowPtr
  86.  
  87. OSErr    MakeWindowObj(WindowPtr theWindow, AEDesc *result)
  88. {
  89.     AEDesc    nullDesc = {typeNull, NULL},
  90.             absoluteDesc = {typeNull, NULL};
  91.     long    windowIndex;
  92.     OSErr    err;
  93.     
  94.     windowIndex = GetNthWindowOfWindowPtr(theWindow);
  95.     if (! windowIndex)
  96.         return(errAENoSuchObject);
  97.     
  98.     err = AECreateDesc(typeInteger,(Ptr)&windowIndex, sizeof(windowIndex), &absoluteDesc);
  99.     if (noErr != err) goto done;
  100.     
  101.     err = CreateObjSpecifier(cWindow, &nullDesc, formAbsolutePosition,
  102.                                                 &absoluteDesc, false, result);
  103.  
  104. done:
  105.     if (absoluteDesc.dataHandle)
  106.         AEDisposeDesc(&absoluteDesc);
  107.     
  108.     return(err);
  109. } // MakeWindowObj
  110.  
  111. // Make an object specifier for a document given the WindowPtr
  112.  
  113. OSErr    MakeDocumentObj(WindowPtr theWindow, AEDesc *result)
  114. {
  115.     AEDesc    nullDesc = {typeNull, NULL},
  116.             absoluteDesc = {typeNull, NULL};
  117.     long    windowIndex;
  118.     OSErr    err;
  119.     
  120.     windowIndex = GetNthWindowOfWindowPtr(theWindow);
  121.     if (! windowIndex)
  122.         return(errAENoSuchObject);
  123.     
  124.     err = AECreateDesc(typeInteger,(Ptr)&windowIndex, sizeof(windowIndex), &absoluteDesc);
  125.     if (noErr != err) goto done;
  126.     
  127.     err = CreateObjSpecifier(cDocument, &nullDesc, formAbsolutePosition,
  128.                                                 &absoluteDesc, false, result);
  129.  
  130. done:
  131.     if (absoluteDesc.dataHandle)
  132.         AEDisposeDesc(&absoluteDesc);
  133.     
  134.     return(err);
  135. } // MakeDocumentObj
  136.  
  137.  
  138. OSErr    MakeTextObjFromToken(TextToken* theToken, AEDesc* result)
  139. {
  140.     OSErr    err;
  141.  
  142.     err = GetTextTokenObjectSpecifier(theToken, result);
  143.     
  144.     return(err);
  145. }
  146.  
  147.  
  148. OSErr    MakeSelectedTextObj(WindowPtr theWindow, TEHandle theTextEditHandle, AEDesc* result)
  149. {
  150.     return( MakeTextObj(theWindow, (**theTextEditHandle).selStart,
  151.                             (**theTextEditHandle).selEnd, result));
  152. } // MakeSelectedTextObj
  153.  
  154.  
  155. OSErr    MakeTextObj(WindowPtr theWindow, short selStart, short selEnd, AEDesc* result)
  156. {
  157.     TextToken    aToken;
  158.     OSErr        err;
  159.     
  160.     aToken.tokenWindow = theWindow;
  161.     aToken.tokenOffset = selStart + 1;
  162.     aToken.tokenLength = selEnd - selStart;
  163.  
  164.     err = MakeTextObjFromToken(&aToken, result);
  165.  
  166.     return(err);
  167. }
  168.  
  169. OSErr    SendSelectionEvent(DPtr docPtr)
  170. {
  171.     AEAddressDesc    ourAddress = {typeNull, NULL};
  172.     AppleEvent        selectEvent = {typeNull, NULL},
  173.                     ignoreReply = {typeNull, NULL};
  174.     AEDesc            textObj = {typeNull, NULL};
  175.     OSErr            err;
  176.  
  177.  
  178.     err = MakeSelfAddress(&ourAddress);
  179.     if (noErr != err) goto done;
  180.     
  181.         // Build an object to represent the current document's selection
  182.         // MakeSelectedTextObj
  183.     err = MakeSelectedTextObj(docPtr->theWindow, docPtr->theText, &textObj);
  184.     if (noErr != err) goto done;
  185.     
  186.     err = AECreateAppleEvent(kAEMiscStandards, kAESelect, &ourAddress, 0, 0, &selectEvent);    
  187.     if (noErr != err) goto done;
  188.     
  189.         // add parameter
  190.     err = AEPutParamDesc(&selectEvent, keyDirectObject, &textObj);
  191.     if (noErr != err) goto done;
  192.                     
  193.         // and now send the message
  194.     err = AESend(&selectEvent, &ignoreReply, kAENoReply, kAEHighPriority, kAEDefaultTimeout, NULL, NULL);
  195.     if (noErr != err) goto done;
  196.  
  197. done:    
  198.     if (ourAddress.dataHandle) 
  199.         AEDisposeDesc(&ourAddress);
  200.     if (selectEvent.dataHandle) 
  201.         AEDisposeDesc(&selectEvent);
  202.     if (ignoreReply.dataHandle) 
  203.         AEDisposeDesc(&ignoreReply);
  204.     if (textObj.dataHandle) 
  205.         AEDisposeDesc(&textObj);
  206.  
  207.     return(err);
  208. }
  209.  
  210. void    DoEditCommand(DPtr theDocument, editCommandType whatCommand)
  211. {
  212.     AEAddressDesc    ourAddress = {typeNull, NULL};
  213.     AppleEvent        editCommandEvent = {typeNull, NULL},
  214.                     ignoreReply = {typeNull, NULL};
  215.     AEEventID        theEventID;
  216.     AEEventClass    theEventClass;
  217.     OSErr            err;
  218.     
  219.     err = SendSelectionEvent(theDocument);
  220.     if (noErr != err) goto done;
  221.  
  222.         // Now create and send the appropriate cut, copy, paste or clear AppleEvent
  223.     
  224.     switch (whatCommand)
  225.     {
  226.         case  editCutCommand:
  227.             theEventID = kAECut;
  228.             theEventClass = kAEMiscStandards;
  229.             break;
  230.             
  231.         case  editCopyCommand:
  232.             theEventID = kAECopy;
  233.             theEventClass = kAEMiscStandards;
  234.             break;
  235.  
  236.         case  editPasteCommand:
  237.             theEventID = kAEPaste;
  238.             theEventClass = kAEMiscStandards;
  239.             break;
  240.  
  241.         case  editClearCommand:
  242.             theEventID = kAEDelete;
  243.             theEventClass = kAECoreSuite;
  244.             break;
  245.     }
  246.     
  247.     err = MakeSelfAddress(&ourAddress);
  248.     if (noErr != err) goto done;
  249.             
  250.     err = AECreateAppleEvent(theEventClass, theEventID, &ourAddress, 0, 0, &editCommandEvent);    
  251.     if (noErr != err) goto done;
  252.             
  253.         // and now Send the message
  254.     err = AESend(&editCommandEvent, &ignoreReply, kAENoReply, kAEHighPriority, kAEDefaultTimeout, NULL, NULL);
  255.         
  256. done:
  257.     if (ourAddress.dataHandle) 
  258.         AEDisposeDesc(&ourAddress);
  259.     if (editCommandEvent.dataHandle) 
  260.         AEDisposeDesc(&editCommandEvent);
  261.     if (ignoreReply.dataHandle) 
  262.         AEDisposeDesc(&ignoreReply);
  263.  
  264. } // DoEditCommand
  265.  
  266. void    IssueCutCommand(DPtr theDocument)
  267. {            
  268.     DoEditCommand(theDocument, editCutCommand);
  269.  
  270. void    IssueCopyCommand(DPtr theDocument)
  271. {
  272.     DoEditCommand(theDocument, editCopyCommand);
  273. }
  274.  
  275. void    IssuePasteCommand(DPtr theDocument)
  276. {
  277.     DoEditCommand(theDocument, editPasteCommand);    
  278. }
  279.  
  280. void    IssueClearCommand(DPtr theDocument)
  281. {
  282.     DoEditCommand(theDocument, editClearCommand);    
  283. }
  284.  
  285. // ---------------------------------------------------------------------
  286. //    Name :         SendAESetObjProp
  287. //    Function :    Creates a property object from an object,
  288. //                a property type and its data and sends it to
  289. //                the requested address, and cleans up zapping params too
  290. // ---------------------------------------------------------------------
  291.  
  292. OSErr    SendAESetObjProp(AEDesc *theObj, DescType theProp, AEDesc *theData, AEAddressDesc *toWhom)
  293. {
  294.     AEDesc        propObjSpec;
  295.     AppleEvent    myAppleEvent;
  296.     AppleEvent    defReply;
  297.     OSErr        myErr;
  298.     OSErr        ignoreErr;
  299.     AEDesc        theProperty;
  300.         
  301.     // create an object spec that represents the property of the given object
  302.     
  303.     myErr = AECreateDesc(typeType, (Ptr)&theProp, sizeof(theProp), &theProperty);
  304.     if (myErr==noErr)
  305.         myErr = CreateObjSpecifier(cProperty, theObj, formPropertyID,
  306.                                         &theProperty, true, &propObjSpec);    
  307.         
  308.     // create event
  309.     
  310.     if (myErr==noErr)
  311.         myErr = AECreateAppleEvent(kAECoreSuite, kAESetData, toWhom, 0, 0, &myAppleEvent);
  312.         
  313.     // add prop obj spec to the event
  314.     
  315.     if (myErr==noErr)
  316.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &propObjSpec);
  317.     
  318.     // add prop data to the event
  319.     
  320.     if (myErr==noErr)
  321.         myErr = AEPutParamDesc(&myAppleEvent,keyAEData, theData);
  322.     
  323.     // send event
  324.     
  325.     if (myErr==noErr)
  326.         myErr = AESend(&myAppleEvent, &defReply, kAENoReply+kAEAlwaysInteract,
  327.                                     kAENormalPriority, kAEDefaultTimeout, nil, nil);
  328.     
  329.     if (myAppleEvent.dataHandle)
  330.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  331.         
  332.     if (&propObjSpec.dataHandle)
  333.       ignoreErr = AEDisposeDesc(&propObjSpec);
  334.     
  335.     if (theData->dataHandle)
  336.         ignoreErr = AEDisposeDesc(theData);
  337.     
  338.     if (toWhom->dataHandle)
  339.         ignoreErr = AEDisposeDesc(toWhom);
  340.     
  341.     return(myErr);
  342. } // SendAESetObjProp
  343.  
  344.  
  345. void    IssueFontCommand(DPtr theDocument, short theItem)
  346. {
  347.     Str255            name;
  348.     AEDesc            strDesc;
  349.     AEAddressDesc    theAddress;
  350.     AEDesc            selTextObj;
  351.     OSErr            err;
  352.         
  353.     err = MakeSelfAddress(&theAddress);
  354.     
  355.     err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);
  356.     
  357.     GetMenuItemText(myMenus[fontM], theItem, name);
  358.     
  359.     if (err==noErr)
  360.         err  = AECreateDesc(typeChar, (Ptr)&name[1], name[0], &strDesc);
  361.     
  362.     if (err==noErr)
  363.         err  = SendAESetObjProp(&selTextObj, pFont, &strDesc, &theAddress);                                                            
  364. }
  365.  
  366. // Window property routines
  367.  
  368. void    IssueZoomCommand(WindowPtr whichWindow, short whichPart)
  369. {
  370.     Boolean       zoomBool;
  371.     AEDesc        zoomDesc;
  372.     AEAddressDesc selfAddr;
  373.     AEDesc        frontWinObj;
  374.     OSErr         err;
  375.  
  376.     err = MakeSelfAddress(&selfAddr);
  377.     
  378.     err = MakeWindowObj(whichWindow, &frontWinObj);
  379.     
  380.     zoomBool = (whichPart==inZoomOut);
  381.  
  382.     err = AECreateDesc(typeBoolean, (Ptr)&zoomBool, sizeof(zoomBool), &zoomDesc);
  383.                                             
  384.     err = SendAESetObjProp(&frontWinObj, pIsZoomed, &zoomDesc, &selfAddr);                                                            
  385. } // IssueZoomCommand
  386.  
  387. void    IssueCloseCommand(WindowPtr whichWindow)
  388. {
  389.     AEAddressDesc  selfAddr;
  390.     AEDesc         frontWinObj;
  391.     OSErr          err;
  392.     OSErr          ignoreErr;
  393.     AppleEvent     closeCommandEvent;
  394.     AppleEvent     ignoreReply;
  395.  
  396.     frontWinObj.dataHandle = nil;
  397.     
  398.     err = MakeSelfAddress(&selfAddr);
  399.     
  400.     err = MakeWindowObj(whichWindow, &frontWinObj);
  401.                                                         
  402.     err = AECreateAppleEvent( kAECoreSuite, kAEClose, &selfAddr, 0, 0, &closeCommandEvent) ;                
  403.     
  404.     // add parameter - the window to close    
  405.     if (err==noErr) 
  406.         err = AEPutParamDesc(&closeCommandEvent, keyDirectObject, &frontWinObj);
  407.         
  408.     if (err==noErr) 
  409.         err = AESend(&closeCommandEvent,&ignoreReply,kAENoReply+kAEAlwaysInteract,kAEHighPriority,10000,nil, nil);
  410.     
  411.     if (closeCommandEvent.dataHandle) 
  412.         ignoreErr = AEDisposeDesc(&closeCommandEvent);
  413.     
  414.     if (selfAddr.dataHandle) 
  415.         ignoreErr = AEDisposeDesc(&selfAddr);
  416.         
  417.     if (frontWinObj.dataHandle) 
  418.         ignoreErr = AEDisposeDesc(&frontWinObj);
  419.         
  420. } // IssueCloseCommand
  421.  
  422. void    IssueSizeWindow(WindowPtr whichWindow, short newHSize, short newVSize)
  423. {
  424.     Rect          sizeRect;
  425.     Rect          contentRect;
  426.     short         edgeSize;
  427.     AEDesc        sizeDesc;
  428.     AEAddressDesc selfAddr;
  429.     AEDesc        frontWinObj;
  430.     OSErr         err;
  431.  
  432.     sizeRect    = (**(((WindowPeek)whichWindow)->strucRgn)).rgnBBox;
  433.     contentRect = (**(((WindowPeek)whichWindow)->contRgn)).rgnBBox;
  434.     
  435.     edgeSize = sizeRect.right-sizeRect.left-(contentRect.right-contentRect.left);
  436.     sizeRect.right = sizeRect.left+newHSize+edgeSize;
  437.     
  438.     edgeSize = sizeRect.bottom-sizeRect.top-(contentRect.bottom-contentRect.top);
  439.     sizeRect.bottom = sizeRect.top+newVSize+edgeSize;
  440.     
  441.     err = MakeSelfAddress(&selfAddr);
  442.     
  443.     err = MakeWindowObj(whichWindow, &frontWinObj);
  444.     
  445.     if (err==noErr)
  446.         err = AECreateDesc(typeQDRectangle, (Ptr)&sizeRect, sizeof(sizeRect), &sizeDesc);
  447.     
  448.     if (err==noErr)
  449.         err  = SendAESetObjProp(&frontWinObj, pBounds, &sizeDesc, &selfAddr);                                                            
  450. } // IssueSizeWindow
  451.  
  452. void    IssueMoveWindow(WindowPtr whichWindow, Rect sizeRect)
  453. {
  454.     AEDesc        sizeDesc;
  455.     AEAddressDesc selfAddr;
  456.     AEDesc        frontWinObj;
  457.     OSErr         err;
  458.  
  459.     err = MakeSelfAddress(&selfAddr);
  460.     
  461.     err = MakeWindowObj(whichWindow, &frontWinObj);
  462.         
  463.     if (err==noErr)
  464.         err = AECreateDesc(typeQDRectangle, (Ptr)&sizeRect, sizeof(sizeRect), &sizeDesc);
  465.     
  466.     if (err==noErr)        
  467.         err = SendAESetObjProp(&frontWinObj, pBounds, &sizeDesc, &selfAddr);                                                            
  468. } // IssueMoveWindow
  469.  
  470. void    IssuePageSetupWindow(WindowPtr whichWindow, TPrint thePageSetup)
  471. {
  472.     AEDesc        sizeDesc;
  473.     AEAddressDesc selfAddr;
  474.     AEDesc        frontWinObj;
  475.     OSErr         err;
  476.  
  477.     err = MakeSelfAddress(&selfAddr);
  478.     
  479.     err = MakeWindowObj(whichWindow, &frontWinObj);
  480.     
  481.     if (err==noErr)
  482.         err = AECreateDesc(typeTPrint, (Ptr)&thePageSetup, sizeof(thePageSetup), &sizeDesc);
  483.                                              
  484.     if (err==noErr)
  485.         err = SendAESetObjProp(&frontWinObj, pPageSetup, &sizeDesc, &selfAddr);                                                            
  486.                                                  
  487. } //IssuePageSetupWindow
  488.  
  489.  
  490. void IssuePrintWindow(WindowPtr whichWindow, Boolean useDialog)
  491. {
  492.     AEAddressDesc selfAddr;
  493.     AEDesc        frontWinObj;
  494.     OSErr         err;
  495.     OSErr         ignoreErr;
  496.     AppleEvent    printCommandEvent;
  497.     AppleEvent    ignoreReply;
  498.     AESendMode    sendModeFlags;
  499.  
  500.     err = MakeSelfAddress(&selfAddr);
  501.     
  502.     err = MakeWindowObj(whichWindow, &frontWinObj);
  503.                                                         
  504.     err = AECreateAppleEvent(kCoreEventClass, kAEPrintDocuments, &selfAddr, 0, 0, &printCommandEvent) ;                
  505.  
  506.     //    add parameter - the window to print
  507.  
  508.     if (err==noErr) 
  509.         err = AEPutParamDesc(&printCommandEvent, keyDirectObject, &frontWinObj);
  510.         
  511.     if (err==noErr)
  512.     {
  513.         sendModeFlags = kAENoReply;
  514.         if (useDialog)
  515.             sendModeFlags = sendModeFlags + kAEAlwaysInteract;
  516.         else
  517.             sendModeFlags = sendModeFlags + kAENeverInteract;
  518.         err = AESend(&printCommandEvent,&ignoreReply,sendModeFlags,kAEHighPriority,10000,nil, nil);
  519.     }
  520.     
  521.     if (printCommandEvent.dataHandle)
  522.         ignoreErr = AEDisposeDesc(&printCommandEvent);
  523.     
  524.     if (frontWinObj.dataHandle) 
  525.         err = AEDisposeDesc(&frontWinObj);
  526.         
  527.     if (selfAddr.dataHandle) 
  528.         err = AEDisposeDesc(&selfAddr);
  529.         
  530. } // IssuePrintWindow
  531.  
  532. OSErr    IssueAEOpenDoc(FSSpec myFSSpec)
  533. // send OpenDocs AppleEvent to myself, with a one-element list
  534. // containing the given file spec
  535. //
  536. // NOTES : the core AEOpenDocs event is defined as taking a list of
  537. //        aliases (not file specs) as its direct parameter.  However,
  538. //        we can send the file spec instead and depend on AppleEvents'
  539. //        automatic coercion.  In fact, we don't really even have to put 
  540. //        in a list; AppleEvents will coerce a descriptor into a 1-element
  541. //        list if called for.  In this routine, though, we'll make the
  542. //        list for demonstration purposes.
  543. {
  544.     AppleEvent    myAppleEvent;
  545.     AppleEvent    defReply;
  546.     AEDescList    docList;
  547.     AEAddressDesc selfAddr;
  548.     OSErr         myErr;
  549.     OSErr         ignoreErr;
  550.     
  551.     myAppleEvent.dataHandle = nil;
  552.     docList.dataHandle  = nil;
  553.     selfAddr.dataHandle = nil;
  554.     defReply.dataHandle = nil;
  555.         
  556.     //    Create empty list and add one file spec
  557.  
  558.     myErr = AECreateList(nil,0,false, &docList);
  559.     
  560.     if (myErr==noErr) 
  561.         myErr = AEPutPtr(&docList,1,typeFSS,(Ptr)&myFSSpec,sizeof(myFSSpec));
  562.         
  563.     //    Create a self address to send it to
  564.  
  565.     if (myErr==noErr) 
  566.         myErr = MakeSelfAddress(&selfAddr);
  567.         
  568.     if (myErr==noErr) 
  569.         myErr = AECreateAppleEvent(kCoreEventClass, kAEOpenDocuments, &selfAddr,
  570.                                 kAutoGenerateReturnID,  kAnyTransactionID, &myAppleEvent);
  571.  
  572.     //    Put Params into our event and send it
  573.  
  574.     if (myErr == noErr) 
  575.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &docList);
  576.  
  577.     myErr = AESend(&myAppleEvent, &defReply, kAENoReply+kAEAlwaysInteract, kAENormalPriority,
  578.                                                                      kAEDefaultTimeout, nil, nil);
  579.         
  580.     if (selfAddr.dataHandle) 
  581.         ignoreErr = AEDisposeDesc(&selfAddr);
  582.         
  583.     if (myAppleEvent.dataHandle) 
  584.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  585.         
  586.     if (docList.dataHandle) 
  587.         ignoreErr = AEDisposeDesc(&docList);
  588.         
  589.     return(myErr);
  590.     
  591. } // IssueAEOpenDoc
  592.  
  593. void    IssueAENewWindow(void)
  594. // send the New Element event to myself with a null container
  595. {
  596.     AppleEvent    myAppleEvent;
  597.     AppleEvent    defReply;
  598.     AEAddressDesc selfAddr;
  599.     OSErr         myErr;
  600.     OSErr         ignoreErr;
  601.     DescType      elemClass;
  602.     
  603.     myAppleEvent.dataHandle = nil;
  604.     
  605.     //    Create the address of us
  606.     
  607.     myErr = MakeSelfAddress(&selfAddr);
  608.     
  609.     //    create event 
  610.     
  611.     myErr = AECreateAppleEvent(kAECoreSuite, kAECreateElement, &selfAddr,
  612.                                     kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  613.     
  614.     //    attach desired class of new element
  615.     
  616.     elemClass = cWindow;
  617.     
  618.     if (myErr == noErr) 
  619.         myErr = AEPutParamPtr(&myAppleEvent, keyAEObjectClass, typeType,
  620.                                         (Ptr)&elemClass, sizeof(elemClass));
  621.         
  622.     //    send the event 
  623.     
  624.     if (myErr == noErr) 
  625.         myErr = AESend(&myAppleEvent, &defReply, kAENoReply+kAENeverInteract,
  626.                             kAENormalPriority, kAEDefaultTimeout, nil, nil);
  627.     //    Clean up - reply never created so don't throw away
  628.  
  629.     if (selfAddr.dataHandle) 
  630.         ignoreErr = AEDisposeDesc(&selfAddr);
  631.         
  632.     if (myAppleEvent.dataHandle) 
  633.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  634.                 
  635. } // IssueAENewWindow
  636.  
  637. OSErr    IssueSaveCommand(WindowPtr theWindow, FSSpecPtr where)
  638. // send an AppleEvent Save Event to myself
  639. {
  640.     AEDesc        windowObj;
  641.     AppleEvent    myAppleEvent;
  642.     AppleEvent    defReply;
  643.     OSErr         myErr;
  644.     OSErr         ignoreErr;
  645.     AEAddressDesc selfAddr;
  646.         
  647.     windowObj.dataHandle = nil;
  648.     myAppleEvent.dataHandle = nil;
  649.     
  650.     myErr = MakeWindowObj(theWindow, &windowObj);
  651.             
  652.     if (myErr==noErr) 
  653.         myErr = MakeSelfAddress(&selfAddr);
  654.     
  655.         //    Build event
  656.  
  657.     if (myErr == noErr) 
  658.         myErr = AECreateAppleEvent(kAECoreSuite, kAESave, &selfAddr,
  659.                             kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  660.   
  661.         //    say which window
  662.  
  663.     if (myErr==noErr) 
  664.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &windowObj);
  665.  
  666.         //    add optional file param if we need to
  667.  
  668.     if (where) 
  669.         if (myErr==noErr) 
  670.             myErr = AEPutParamPtr(&myAppleEvent, keyAEDestination, typeFSS,
  671.                                                     (Ptr)where, sizeof(FSSpec));
  672.     
  673.         // send the event
  674.  
  675.     if (myErr==noErr) 
  676.         myErr  = AESend(&myAppleEvent, &defReply, kAENoReply+kAENeverInteract,
  677.                                     kAENormalPriority, kAEDefaultTimeout, nil, nil);
  678.       
  679.     if (selfAddr.dataHandle) 
  680.         ignoreErr = AEDisposeDesc(&selfAddr);
  681.       
  682.     if (windowObj.dataHandle) 
  683.         ignoreErr = AEDisposeDesc(&windowObj);
  684.         
  685.     if (myAppleEvent.dataHandle) 
  686.         myErr = AEDisposeDesc(&myAppleEvent);
  687.         
  688.     return(myErr);
  689. }    // IssueSaveCommand
  690.  
  691. OSErr    IssueRevertCommand(WindowPtr theWindow)
  692.     // send an AppleEvent Revert Event to myself
  693. {
  694.     AEDesc        windowObj;
  695.     AppleEvent    myAppleEvent;
  696.     AppleEvent    defReply;
  697.     OSErr         myErr;
  698.     OSErr         ignoreErr;
  699.     AEAddressDesc selfAddr;
  700.     
  701.     windowObj.dataHandle = nil;
  702.     myAppleEvent.dataHandle = nil;
  703.     
  704.     myErr = MakeWindowObj(theWindow, &windowObj);
  705.                 
  706.     if (myErr==noErr) 
  707.         myErr = MakeSelfAddress(&selfAddr);
  708.         
  709.     // Build event
  710.     
  711.     if (myErr == noErr) 
  712.         myErr  = AECreateAppleEvent(kAEMiscStandards, kAERevert, &selfAddr,
  713.                                     kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  714.     // say which window
  715.     
  716.     if (myErr == noErr) 
  717.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, &windowObj);
  718.     //    send the event
  719.  
  720.     if (myErr==noErr) 
  721.         myErr  = AESend(&myAppleEvent, &defReply, kAENoReply+kAENeverInteract,
  722.                                 kAENormalPriority, kAEDefaultTimeout, nil, nil);
  723.         
  724.     if (windowObj.dataHandle) 
  725.         ignoreErr = AEDisposeDesc(&windowObj);
  726.         
  727.     if (myAppleEvent.dataHandle) 
  728.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  729.         
  730.     if (selfAddr.dataHandle) 
  731.         ignoreErr = AEDisposeDesc(&selfAddr);
  732.         
  733.     return(myErr);
  734. } // IssueRevertCommand
  735.  
  736. // ----------------------------------------------------
  737. //    Name :         IssueQuitCommand
  738. //    Purpose :    Sends self a Quit AppleEvent
  739. // ----------------------------------------------------
  740. OSErr    IssueQuitCommand(void)
  741. {
  742.     AppleEvent    myAppleEvent;
  743.     AppleEvent    defReply;
  744.     OSErr         myErr;
  745.     OSErr         ignoreErr;
  746.     AEAddressDesc selfAddr;
  747.     DescType      mySaveOpt;
  748.     
  749.     myAppleEvent.dataHandle = nil;
  750.     selfAddr.dataHandle     = nil;
  751.                         
  752.     myErr = MakeSelfAddress(&selfAddr);
  753.         
  754.     //    Build event
  755.     
  756.     if (myErr == noErr) 
  757.         myErr  = AECreateAppleEvent(kCoreEventClass, kAEQuitApplication, &selfAddr,
  758.                                         kAutoGenerateReturnID, kAnyTransactionID, &myAppleEvent);
  759.     //    say which save option
  760.     
  761.     mySaveOpt = kAEAsk;
  762.     
  763.     if (myErr == noErr) 
  764.         myErr = AEPutParamPtr(&myAppleEvent, keyAESaveOptions, typeEnumerated,
  765.                                                 (Ptr)&mySaveOpt, sizeof(mySaveOpt));
  766.     //    send the event
  767.  
  768.     if (myErr==noErr) 
  769.         myErr  = AESend(&myAppleEvent, &defReply, kAENoReply+kAEAlwaysInteract,
  770.                                     kAENormalPriority, kAEDefaultTimeout, nil, nil);
  771.                     
  772.     if (myAppleEvent.dataHandle) 
  773.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  774.         
  775.     if (selfAddr.dataHandle) 
  776.         ignoreErr = AEDisposeDesc(&selfAddr);
  777.         
  778.     return(myErr);
  779. } // IssueQuitCommand
  780.  
  781. #define kOK 1
  782. #define kCancel 2
  783. #define kOtherSize 4
  784. #define kOutlineItem 5
  785.  
  786. Boolean    PoseSizeDialog(long *whatSize)
  787. {
  788.     GrafPtr   savedPort;
  789.     DialogPtr aDialog;
  790.     Str255    aString;
  791.     short     itemHit;
  792.  
  793.     GetPort(&savedPort);
  794.     aDialog = GetNewDialog(1004, nil, (WindowPtr)-1);
  795.     ShowWindow(aDialog);
  796.     SetPort(aDialog);
  797.     
  798.     AdornDefaultButton(aDialog, kOutlineItem);
  799.      
  800.         //set the edittext button to contain the right size
  801.     NumToString(*whatSize, aString);
  802.     SetText(aDialog, kOtherSize, aString);
  803.  
  804.     do
  805.     {
  806.         ModalDialog(nil, &itemHit);
  807.     } while ((itemHit!=kOK) && (itemHit!=kCancel));
  808.     
  809.     if (itemHit == kOK) 
  810.         RetrieveText(aDialog, kOtherSize, aString);
  811.  
  812.     DisposeDialog(aDialog);
  813.     SetPort(savedPort);
  814.     
  815.     if (itemHit == kOK) 
  816.     {
  817.         // set the new size of the text
  818.         StringToNum(aString, whatSize);
  819.         if ((*whatSize<1) || (*whatSize>2000)) 
  820.             *whatSize = 12;
  821.     }
  822.  
  823.     return(itemHit == kOK);
  824. }
  825.  
  826. void    IssueSizeCommand(DPtr theDocument,short theItem)
  827. {
  828.     Str255        name;
  829.     AEDesc        sizeDesc;
  830.     AEAddressDesc theAddress;
  831.     OSErr         err;
  832.     AEDesc        selTextObj;
  833.     
  834.         // Vars to do with menu processing
  835.     short     lastSize;
  836.     short     upItem;
  837.     short     downItem;
  838.     short     otherItem;
  839.     long      theSize;
  840.     TextStyle theStyle;
  841.     short     lineHeight;
  842.     short     fontAscent;
  843.         
  844.     err = MakeSelfAddress(&theAddress);
  845.     
  846.     err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);        
  847.  
  848.         // check if the item is on the Size menu
  849.         // remembering that we can add and delete items from it
  850.     lastSize  = CountMItems(myMenus[sizeM]) - 5;
  851.     upItem    = lastSize + 2;
  852.     downItem  = upItem + 1;
  853.     otherItem = downItem + 2;
  854.   
  855.     TEGetStyle((**(theDocument->theText)).selStart, &theStyle, &lineHeight,
  856.                                             &fontAscent, theDocument->theText);
  857.     
  858.     GetMenuItemText(myMenus[sizeM], theItem, name);
  859.  
  860.     if (theItem <= lastSize)
  861.     {
  862.         GetMenuItemText(myMenus[sizeM], theItem, name);
  863.         StringToNum(name, &theSize);
  864.     }
  865.     else if (theItem == upItem)  
  866.         theSize = theStyle.tsSize + 1;
  867.     else if (theItem == downItem)
  868.         theSize = theStyle.tsSize - 1;
  869.     else if (theItem == otherItem) 
  870.     {
  871.         theSize = theStyle.tsSize;
  872.         if (!PoseSizeDialog(&theSize))
  873.             return;
  874.     }
  875.  
  876.     if (err==noErr)
  877.         err = CreateOffsetDescriptor(theSize, &sizeDesc);
  878.     
  879.     if (err==noErr)
  880.         err = SendAESetObjProp(&selTextObj, pPointSize, &sizeDesc, &theAddress);
  881. } // IssueSizeCommand
  882.  
  883. void    IssueStyleCommand(DPtr theDocument, short theItem)
  884. {
  885.     Style         theFace;
  886.     OSErr         err;
  887.     AEDesc        result;
  888.     AEAddressDesc selfAddr;
  889.     AEDesc        selTextObj;
  890.     TextStyle     theStyle;
  891.     short         lineHeight;
  892.     short         fontAscent;
  893.         
  894.     TEGetStyle((**(theDocument->theText)).selStart, &theStyle,
  895.                     &lineHeight, &fontAscent, theDocument->theText);
  896.     
  897.     theFace = 0;
  898.  
  899.     switch (theItem)
  900.     {
  901.         case cPlain:
  902.             theFace = 0;
  903.             break;
  904.         case cBold:
  905.             theFace = bold;
  906.             break;
  907.         case cItalic:
  908.             theFace = italic;
  909.             break;
  910.         case cUnderline:
  911.             theFace = underline;
  912.             break;
  913.         case cOutline:
  914.             theFace = outline;
  915.             break;
  916.         case cShadow:
  917.             theFace = shadow;
  918.             break;
  919.         case cCondense:
  920.             theFace = condense;
  921.             break;
  922.         case cExtend:
  923.             theFace = extend;
  924.             break;
  925.     } // of switch
  926.  
  927.     if (theFace==0)
  928.         err = BuildTypeTextStylesDesc(0, bold+italic+underline+outline+shadow+condense+extend, &result);
  929.     else if (theFace & theStyle.tsFace)
  930.         err = BuildTypeTextStylesDesc(0, theFace, &result);
  931.     else
  932.         err = BuildTypeTextStylesDesc(theFace, 0, &result);
  933.     
  934.     err = MakeSelfAddress(&selfAddr);
  935.     
  936.     if (err==noErr)
  937.         err = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText, &selTextObj);
  938.     
  939.     if (err==noErr)
  940.         err = SendAESetObjProp(&selTextObj, pTextStyles, &result, &selfAddr);
  941. } // IssueStyleCommand
  942.  
  943.  
  944. OSErr    IssueSetDataObjToBufferContents(const AEDesc* theObj)
  945.     OSErr            myErr;
  946.     OSErr            ignoreErr;
  947.     AEAddressDesc    theAddress;
  948.     AppleEvent        myAppleEvent;
  949.     AppleEvent        defReply;
  950.  
  951.     myErr = MakeSelfAddress(&theAddress);
  952.     
  953.     // create event
  954.     
  955.     if (myErr==noErr)
  956.         myErr = AECreateAppleEvent(kAECoreSuite, kAESetData, &theAddress,
  957.                                                         0, 0, &myAppleEvent);
  958.         
  959.     // add prop obj spec to the event
  960.     
  961.     if (myErr==noErr)
  962.         myErr = AEPutParamDesc(&myAppleEvent, keyDirectObject, theObj);
  963.     
  964.     // add prop data to the event
  965.     
  966.     if (myErr==noErr)
  967.         myErr = AEPutParamPtr(&myAppleEvent, keyAEData, typeChar,
  968.                                     (Ptr)gTypingBuffer, gCharsInBuffer);
  969.                                                     
  970.     // send event
  971.     
  972.     if (myErr==noErr)
  973.      if (gRecordingImplemented)
  974.          myErr = AESend(&myAppleEvent, &defReply, kAENoReply+kAEDontExecute,
  975.                                      kAENormalPriority, kAEDefaultTimeout, nil, nil);
  976.  
  977.     if (theAddress.dataHandle)
  978.         ignoreErr = AEDisposeDesc(&theAddress);
  979.         
  980.     if (myAppleEvent.dataHandle)
  981.         ignoreErr = AEDisposeDesc(&myAppleEvent);
  982.         
  983.     return(myErr);
  984. }
  985.  
  986. void    AddKeyToTypingBuffer(DPtr theDocument, char theKey)
  987. {
  988.     OSErr myErr;
  989.     OSErr ignoreErr;
  990.     
  991.     if (theKey==BS || theKey==FS || theKey==GS || theKey==RS || theKey==US)
  992.     {
  993.         FlushAndRecordTypingBuffer();
  994.         if (theKey==BS)
  995.         {
  996.             if ((**theDocument->theText).selStart!=(**theDocument->theText).selEnd)
  997.             {
  998.                 myErr = MakeTextObj(theDocument->theWindow,
  999.                                     (**theDocument->theText).selStart,
  1000.                                     (**theDocument->theText).selEnd,
  1001.                                     &gTypingTargetObject);
  1002.             }
  1003.             else
  1004.             {
  1005.                 myErr = MakeTextObj(theDocument->theWindow,
  1006.                                     (**theDocument->theText).selStart-1,
  1007.                                     (**theDocument->theText).selStart,
  1008.                                     &gTypingTargetObject);
  1009.             }
  1010.                 
  1011.             myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  1012.             
  1013.             ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  1014.             
  1015.             gTypingTargetObject.dataHandle = nil;
  1016.         }
  1017.     }
  1018.     else
  1019.     {
  1020.         if (gCharsInBuffer==0)
  1021.             myErr = MakeSelectedTextObj(theDocument->theWindow, theDocument->theText,
  1022.                                                                     &gTypingTargetObject);
  1023.  
  1024.         gTypingBuffer[gCharsInBuffer++] = theKey;
  1025.     }
  1026. }
  1027.  
  1028. void    FlushAndRecordTypingBuffer(void)
  1029.     OSErr  myErr;
  1030.     OSErr  ignoreErr;
  1031.  
  1032.     if (gCharsInBuffer != 0)
  1033.     {
  1034.         myErr = IssueSetDataObjToBufferContents(&gTypingTargetObject);
  1035.         
  1036.         if (gTypingTargetObject.dataHandle)
  1037.             ignoreErr = AEDisposeDesc(&gTypingTargetObject);
  1038.     }
  1039.         
  1040.     gCharsInBuffer = 0;
  1041.     gTypingTargetObject.dataHandle = 0;
  1042. }
  1043.  
  1044.     
  1045. void    StyleTokConst(short theStyleItem, DescType *thekConst)
  1046. {
  1047.     switch (theStyleItem)
  1048.     {
  1049.         case bold:
  1050.             *thekConst = kAEBold;
  1051.             break;
  1052.             
  1053.         case italic:
  1054.             *thekConst = kAEItalic;
  1055.             break;
  1056.             
  1057.         case underline:
  1058.             *thekConst = kAEUnderline;
  1059.             break;
  1060.             
  1061.         case outline:
  1062.             *thekConst = kAEOutline;
  1063.             break;
  1064.             
  1065.         case shadow:
  1066.             *thekConst = kAEShadow;
  1067.             break;
  1068.             
  1069.         case condense:
  1070.             *thekConst = kAECondensed;
  1071.             break;
  1072.             
  1073.         case extend:
  1074.             *thekConst = kAEExpanded;
  1075.             break;
  1076.     }
  1077. } // StyleTokConst
  1078.  
  1079. OSErr    BuildTypeTextStylesDesc(Style onStyles, Style offStyles, AEDesc *resultDesc)
  1080. {
  1081.  
  1082.     OSErr     myErr;
  1083.     OSErr     ignoreErr;
  1084.     short     myStyleItem;
  1085.     DescType  styleConst;
  1086.     AEDesc    onStylesDesc;
  1087.     AEDesc    offStylesDesc;
  1088.     AEDesc    dataDesc;
  1089.     
  1090.     onStylesDesc.dataHandle  = nil;
  1091.     offStylesDesc.dataHandle = nil;
  1092.     dataDesc.dataHandle  = nil;
  1093.  
  1094.     myErr = AECreateList(nil, 0, true,  &dataDesc);
  1095.     
  1096.     myErr = AECreateList(nil, 0, false, &onStylesDesc);
  1097.     myErr = AECreateList(nil, 0, false, &offStylesDesc);
  1098.     
  1099.     for (myStyleItem = bold; myStyleItem<=extend; myStyleItem = myStyleItem <<1)
  1100.         if (myErr==noErr)
  1101.         {
  1102.             StyleTokConst(myStyleItem, &styleConst);
  1103.             if (myStyleItem & onStyles)
  1104.                 myErr = AEPutPtr(&onStylesDesc,
  1105.                                     0,                // add to end of list
  1106.                                     typeEnumerated, // text for style name
  1107.                                     (Ptr)&styleConst,
  1108.                                     sizeof(styleConst));
  1109.             
  1110.             if (myStyleItem & offStyles)
  1111.                 myErr = AEPutPtr(&offStylesDesc,
  1112.                                     0,                // add to end of list
  1113.                                     typeEnumerated, // text for style name
  1114.                                     (Ptr)&styleConst,
  1115.                                     sizeof(styleConst));
  1116.         }
  1117.     
  1118.     if (myErr==noErr)
  1119.         myErr = AEPutKeyDesc(&dataDesc, keyAEOnStyles,  &onStylesDesc);
  1120.     
  1121.     if (myErr==noErr)
  1122.         myErr = AEPutKeyDesc(&dataDesc, keyAEOffStyles, &offStylesDesc);
  1123.     
  1124.     if (myErr==noErr)
  1125.         myErr = AECoerceDesc(&dataDesc, typeTextStyles, resultDesc);
  1126.         
  1127.     if (onStylesDesc.dataHandle)
  1128.         ignoreErr = AEDisposeDesc(&onStylesDesc);
  1129.         
  1130.     if (offStylesDesc.dataHandle)
  1131.         ignoreErr = AEDisposeDesc(&offStylesDesc);
  1132.         
  1133.     if (dataDesc.dataHandle)
  1134.         ignoreErr = AEDisposeDesc(&dataDesc);
  1135.  
  1136.     return(myErr);
  1137. }
  1138.  
  1139. OSErr    BuildTextStylesDesc(Style theStyle, AEDesc *resultDesc)
  1140. {
  1141.     short     myStyleItem;
  1142.     Style     onStyles;
  1143.     Style     offStyles;
  1144.             
  1145.     onStyles  = 0;
  1146.     offStyles = 0;
  1147.     
  1148.     for (myStyleItem = bold; myStyleItem<=extend; myStyleItem = myStyleItem <<1)
  1149.     {
  1150.         if (myStyleItem & theStyle)
  1151.             onStyles  = onStyles  + myStyleItem;
  1152.         else
  1153.             offStyles = offStyles + myStyleItem;
  1154.     }
  1155.  
  1156.     return(BuildTypeTextStylesDesc(onStyles, offStyles, resultDesc));
  1157.     
  1158. } // BuildTextStylesDesc
  1159.         
  1160. OSErr    BuildStyledTextDesc(TEHandle theHTE, short start, short howLong, AEDesc *resultDesc)
  1161. {
  1162.     AEDesc       listDesc;
  1163.     short        oldSelStart;
  1164.     short        oldSelEnd;
  1165.     StScrpHandle myStScrpHandle;
  1166.     OSErr        myErr;
  1167.     OSErr        ignoreErr;
  1168.     
  1169.     listDesc.dataHandle = nil;
  1170.     
  1171.     oldSelStart = (**theHTE).selStart;
  1172.     oldSelEnd   = (**theHTE).selEnd;
  1173.     
  1174.     TESetSelect(start-1, start+howLong-2, theHTE);
  1175.     
  1176.     myErr = AECreateList(nil, 0, true,  &listDesc);
  1177.     
  1178.     HLock((Handle)(**theHTE).hText);
  1179.                                                      
  1180.     if (myErr == noErr)
  1181.         myErr = AEPutKeyPtr(&listDesc, keyAEText, typeChar,
  1182.                             (Ptr)&(*(**theHTE).hText)[start-1], howLong);
  1183.                                                 
  1184.     HUnlock((Handle)(**theHTE).hText);
  1185.     
  1186.     myStScrpHandle = TEGetStyleScrapHandle(theHTE);
  1187.     
  1188.     if (myStScrpHandle)
  1189.     {
  1190.         HLock((Handle)myStScrpHandle);
  1191.         
  1192.         if (myErr==noErr)
  1193.             myErr = AEPutKeyPtr(&listDesc, keyAEStyles, typeScrapStyles,
  1194.                                     (Ptr)*myStScrpHandle, GetHandleSize((Handle)myStScrpHandle));
  1195.             
  1196.         HUnlock((Handle)myStScrpHandle);
  1197.     }    
  1198.     else
  1199.         myErr = AEPutKeyPtr(&listDesc, keyAEStyles, typeScrapStyles, (Ptr)nil, 0);
  1200.     
  1201.     if (myErr==noErr)
  1202.         myErr = AECoerceDesc(&listDesc, typeStyledText, resultDesc); // should be typeIntlText
  1203.     
  1204.     if (listDesc.dataHandle)
  1205.         ignoreErr = AEDisposeDesc(&listDesc);
  1206.     
  1207.     TESetSelect(oldSelStart, oldSelEnd, theHTE);
  1208.     
  1209.     return(myErr);
  1210. }
  1211.